package com.mlm.mlmapp.adapter.view;

import android.content.Context;
import android.view.View;
import android.widget.CompoundButton;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.RadioButton;
import android.widget.TextView;
import android.widget.Toast;

import com.androidquery.AQuery;
import com.androidquery.callback.AjaxCallback;
import com.androidquery.callback.AjaxStatus;
import com.flyco.dialog.widget.base.BaseDialog;
import com.mlm.mlmapp.R;
import com.mlm.mlmapp.adapter.view.listener.DialogListener;
import com.mlm.mlmapp.cons.HTTPCons;
import com.mlm.mlmapp.utils.CommonUtils;

import org.apache.http.NameValuePair;
import org.apache.http.message.BasicNameValuePair;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

public class BuyPartnerDialog extends BaseDialog implements HTTPCons {
	private Context mContext;
	private int layout;
	public static final int PAYMENT_TYPE_ALIPAY = 1;
	public static final int PAYMENT_TYPE_WECHAT = 2;
	List<RadioButton> radioGroup;
	MyListView items;
	List<JSONObject> list = new ArrayList<JSONObject>();
	AQuery aq;
	TextView apply;
	TextView cancel;
	TextView sub_title;
	TextView tv_title;
	EditText tv_reason;
	RadioButton btn_wechat;
	RadioButton btn_alipay;
	ImageView close;
	private int payment_type;
	String mobile;
	int money;
	HashMap<String, String> map = new HashMap<>();
	DialogListener l;
	public BuyPartnerDialog(Context context, DialogListener l, int money, String mobile) {
		super(context);
		// TODO Auto-generated constructor stub
		this.mContext = context;
		this.l = l;
		this.money=money;
		this.mobile=mobile;
		aq = new AQuery(context);
	}

	@Override
	public View onCreateView() {
		// TODO Auto-generated method stub
		View inflate = View.inflate(context, R.layout.buy_points_dialog, null);
		btn_wechat=(RadioButton) inflate.findViewById(R.id.btn_wechat);
		btn_alipay=(RadioButton) inflate.findViewById(R.id.btn_alipay);
		sub_title=(TextView) inflate.findViewById(R.id.sub_title);
		close =(ImageView) inflate.findViewById(R.id.close);
		sub_title.setText(mContext.getString(R.string.money,money));
		radioGroup = new ArrayList<RadioButton>();
		radioGroup.add(btn_wechat);
		radioGroup.add(btn_alipay);

		btn_alipay.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {

			@Override
			public void onCheckedChanged(CompoundButton buttonView,
										 boolean isChecked) {
				// TODO Auto-generated method stub
				if (isChecked) {
					radioGroupChecked(buttonView);
					payment_type = PAYMENT_TYPE_ALIPAY;
				}
			}
		});
		btn_wechat.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {

			@Override
			public void onCheckedChanged(CompoundButton buttonView,
										 boolean isChecked) {
				// TODO Auto-generated method stub
				if (isChecked) {
					radioGroupChecked(buttonView);
					payment_type = PAYMENT_TYPE_WECHAT;
				}
			}
		});
		btn_wechat.setChecked(true);
		close.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View view) {


				BuyPartnerDialog.this.dismiss();
			}
		});
				
//
		apply = (TextView) inflate.findViewById(R.id.btn_buy);
//		cancel = (TextView) inflate.findViewById(R.id.cancel);
		apply.setOnClickListener(new View.OnClickListener() {

			@Override
			public void onClick(View v) {


//				apply.setEnabled(false);
				getPayOrderAction();
			}
		});
		return inflate;
	}

	void getPayOrderAction() {

		List<NameValuePair> pairs = new ArrayList<NameValuePair>();
		pairs.add(new BasicNameValuePair("mobile", mobile));
		pairs.add(new BasicNameValuePair("type", "1"));
		String url;
		if(btn_alipay.isChecked()){
			url=obtion_alipay_partner_order;

		}else{
			url=obtion_wechatpay_partner_order;
		}

		CommonUtils.postRequest(url, aq,
				new AjaxCallback<JSONObject>() {
					@Override
					public void callback(String url, JSONObject obj,
										 AjaxStatus status) {
						if (status.getCode() == -101) {
							Toast.makeText(aq.getContext(), "网络异常,请检查网络",
									Toast.LENGTH_SHORT);
							return;
						}
						if (status.getCode() == -103) {
							Toast.makeText(aq.getContext(), "用户信息异常，请联系客服",
									Toast.LENGTH_SHORT).show();
						} else {
							if (status.getCode() != 200) {
								Toast.makeText(aq.getContext(),
										"网络异常:" + status.getCode(),
										Toast.LENGTH_SHORT).show();
							} else {

								try {
									if (obj.getBoolean("isSuccess")) {
										handlerResult(obj);

									} else {
										Toast.makeText(aq.getContext(),
												obj.getString("errors"),
												Toast.LENGTH_SHORT).show();
									}
								} catch (JSONException e) {
									// TODO Auto-generated catch block
									e.printStackTrace();
								}
							}
						}
					}

				}, pairs);

	}

	void handlerResult(JSONObject object) throws JSONException {
//			Toast.makeText(aq.getContext(),
//					"支付成功！", Toast.LENGTH_SHORT)
//					.show();
			int type=btn_alipay.isChecked()?PAYMENT_TYPE_ALIPAY:PAYMENT_TYPE_WECHAT;
			l.onApply(object,type+"");

	}
	public void setLayout(int layout) {
		this.layout = layout;
	}
	@Override
	public boolean setUiBeforShow() {
		// TODO Auto-generated method stub
		return false;
	}
	private void radioGroupChecked(CompoundButton buttonView) {
		for (RadioButton rb : radioGroup) {
			if (rb != buttonView) {
				rb.setChecked(false);
			}

		}
	}
}
